<!-- 平台审核 -->
<template>
  <div>
    <BaseSearch :config="searchConfig" v-bind="form" ref="search"></BaseSearch>
    <BaseTable :columns="columns" v-bind="table"></BaseTable>
    <AuditPop ref="AuditPop" @closePop="closePop"></AuditPop>
  </div>
</template>

<script>
import BaseSearch from '@/components/BaseSearch/index.vue'
import BaseTable from '@/components/BaseTable/index.vue'
import AuditPop from './components/AuditPop.vue'
import { FormTableMixin } from '@/mixins/form-table'
import http from '@/utils/request'

const service = async params => {
  const { records, total } = await http.get('/credit_applys', {
    params: { ...params, applyType: 0, listType: 3 },
  })
  return {
    data: records,
    total: total,
  }
}

export default {
  name: 'platformAudit',
  components: { BaseSearch, BaseTable, AuditPop },
  mixins: [FormTableMixin(service)],
  data() {
    const isCanDetail = this.$checkAccess('MENU_PLATFORM_AUDIT_DETAL')
    const isCanAudit = this.$checkAccess('MENU_PLATFORM_AUDIT_APPROVAL')
    return {
      statusMap: {
        0: '待审核',
        1: '通过',
        2: '拒绝',
      },
      searchConfig: [
        { component: 'input', prop: 'searchStr', label: '搜索', placeholder: '企业名称/流水号' },
        {
          component: 'select',
          prop: 'fundCode',
          label: '资金方',
          options: [],
        },
        {
          component: 'select',
          prop: 'appCode',
          label: '所属项目',
          options: [],
        },
      ],
      columns: [
        { label: '流水号', prop: 'applyNo' },
        { label: '企业名称', prop: 'tenantName' },
        { label: '产品名称', prop: 'productName' },
        { label: '资金方', prop: 'fundName' },
        { label: '受理日期', prop: 'createDate' },
        {
          label: '审核状态',
          prop: 'platformAuditStatus',
          render: (val, row) => {
            return <div>{this.statusMap[row.platformAuditStatus]}</div>
          },
        },
        {
          label: '准入风险',
          prop: 'failReason',
          render: (val, row) => {
            return (
              <el-popover placement="top-start" width="200" trigger="hover">
                <span slot="reference" style="color:red">
                  {val ? val.length : '--'}
                </span>
                <div>
                  {val &&
                    val.map(function (item, index) {
                      return (
                        <div>
                          {index + 1}. {item}
                        </div>
                      )
                    })}
                </div>
              </el-popover>
            )
          },
        },
        { label: '所属项目', prop: 'appName' },
        { label: '审核人', prop: 'platformAuditName' },
        {
          label: '操作',
          render: (val, row) => {
            return (
              <div>
                {isCanAudit && row.platformAuditStatus === '0' ? (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.audit(row)
                    }}>
                    审核
                  </el-button>
                ) : (
                  ''
                )}

                {isCanDetail && (
                  <el-button
                    type="text"
                    onClick={() => {
                      this.goDetail(row)
                    }}>
                    详情
                  </el-button>
                )}
              </div>
            )
          },
        },
      ],
    }
  },
  methods: {
    audit(item) {
      this.$refs.AuditPop.show(item)
    },
    goDetail(item) {
      const data = JSON.stringify(item)
      this.$router.push({
        name: 'platformAuditDetail',
        query: {
          applyId: item.id,
          appCode: item.appCode,
          uscCode: item.uscCode || '',
          itemData: data || '',
          platformAuditStatus: item.platformAuditStatus,
        },
      })
    },
    closePop(val) {
      if (val) {
        this.$refs.search.$refs.searchForm.resetFields()
        this.form.onSubmit()
      }
    },
    // 资方列表
    async getFundNameRequest() {
      const res = await this.$http.get('/funds/list')
      if (res && res.length) {
        this.searchConfig[1].options = res.map(item => {
          return {
            label: item.name,
            value: item.code,
          }
        })
      }
    },
    // 获取来源
    async getSourceListRequest() {
      const sourceList = await this.$http.get(`/app/list`)
      if (sourceList && sourceList.length) {
        const list = sourceList.map(item => {
          return {
            label: item.name,
            value: item.code,
          }
        })
        this.searchConfig[2].options = [...list]
      }
    },
  },
  mounted() {
    this.getSourceListRequest()
    this.getFundNameRequest()
  },
}
</script>

<style lang="scss" scoped></style>
